<?php
class replaceModel extends baseModel{
    protected $table = 'replace';

    //替换列表
    public function replace_list() {
        return $this->select('','','id desc');
    }

    //替换信息
    public function info($id) {
        return $this->find('id='.$id);
    }

    //添加替换
    public function add($data)
    {
        $data['num']=intval($data['num']);
        return $this->insert($data);
    }
    //编辑替换
    public function edit($data)
    {
        $condition['id']=intval($data['id']);
        $data['num']=intval($data['num']);
        return $this->update($condition,$data); 
    }
    //删除替换
    public function del($id)
    {
        return $this->delete('id='.intval($id)); 
    }

    //替换后内容
    public function replace_content($info=array()){
        $content=$info['content'];
        if(empty($content)){
            return $info;
        }
        $replace = $this->replace_list();
        $content=html_out($content);
        if (!empty($replace)) {
            foreach ($replace as $export) {
                if(!empty($export['key'])){
                    if(empty($export['num'])){
                        $export['num']=1;
                    }
                    $content = preg_replace("/(?!<[^>]+)".preg_quote($export['key'],'/')."(?![^<]*>)/",$export['content'], $content,$export['num']);
                }
            }
        }
        $info['content']=$content;
        return $info;
    }

	
}